﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.DBModule;

namespace ZX.BLL
{
    /// <summary>
    /// ID号
    /// </summary>
    public static class InternalIDMaker
    {
        private static int curNumLength = 8;
        private static string curPreposition = "";
        private static string curPostposition = "";

        public static Int64 GetSequenceNum(string curSequenceName)
        {
            ZX.BLL.Extend.BaseBussnies BInstance = new Extend.BaseBussnies(new Sequence());
            IList<Sequence> seqlist = BInstance.GetEntitys<Sequence>("SequenceName='" + curSequenceName + "'");

            Int64 result = 0;
            Int64 myCurNum = 0;
            Int64 myStep = 0;

            int icount = seqlist == null ? 0 : seqlist.Count();
            if (icount > 0) 
            {
                Sequence seqItem = seqlist.First();
                if (seqItem.CurNum != null)
                    myCurNum = (long)seqItem.CurNum;
                if (seqItem.Step != null)
                    myStep = (long)seqItem.Step;
                result = myCurNum + myStep;
                
                if (result > seqItem.MaxNum)
                    seqItem.CurNum = seqItem.StartNum;
                else
                    seqItem.CurNum = result;


                if (seqItem.NumLength != null)
                    curNumLength = (int)seqItem.NumLength;
                curPostposition = seqItem.Postposition;
                curPreposition = seqItem.Preposition;
                BInstance.entity = seqItem;
                BInstance.Update();
            }
            else
            {
                
                Sequence newItem = new Sequence();
                newItem.SequenceName = curSequenceName;
                newItem.StartNum = 1;
                newItem.Step = 1;
                newItem.MaxNum = 9999999999999;
                newItem.CurNum = 1;
                newItem.Postposition = "";
                newItem.Preposition = "";
                newItem.NumLength = 14;

                if (newItem.CurNum != null)
                    result = (long)newItem.CurNum; 

                if (newItem.NumLength != null)
                    curNumLength = (int)newItem.NumLength;
                curPostposition = newItem.Postposition;
                curPreposition = newItem.Preposition;
                BInstance.entity = newItem;
                BInstance.Insert();
            }

            return result;
        }

        public static string GetFormatSequence(string sequenceName, string preposition, string postposition)
        {
            Int64 curSeqNum = 0;
            string prepNum = "";
            curSeqNum = GetSequenceNum(sequenceName);
            for (int i = 0; i < (curNumLength - curSeqNum.ToString().Length); i++)
            {
                prepNum += "0";
            }
            return preposition + prepNum + curSeqNum.ToString() + postposition;
        }

        public static string GetSequenceStr(string sequenceName)
        {
            Int64 curSeqNum = 0;
            string prepNum = "";
            curSeqNum = GetSequenceNum(sequenceName);
            for (int i = 0; i < (curNumLength - curSeqNum.ToString().Length); i++)
            {
                prepNum += "0";
            }
            return curPreposition + prepNum + curSeqNum.ToString() + curPostposition;
        }
    }

    public class IDMaker
    {
        public static Int64 GetSequenceNum(string curSequenceName)
        {
            lock (typeof(InternalIDMaker))
            {
                return InternalIDMaker.GetSequenceNum(curSequenceName);
            }
        }

        public static string GetFormatSequence(string sequenceName, string preposition, string postposition)
        {
            lock (typeof(InternalIDMaker))
            {
                return InternalIDMaker.GetFormatSequence(sequenceName, preposition, postposition);
            }
        }

        public static string GetSequenceStr(string sequenceName)
        {
            lock (typeof(InternalIDMaker))
            {
                return InternalIDMaker.GetSequenceStr(sequenceName);
            }
        }
    }
}

